package com.ironone.mediaplayerdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

public class Frag1 extends Fragment {

	static Button btn1;
	Button btn2;
	FrameLayout fragContainer;
	ViewPager viewPager;
	View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		fragContainer = (FrameLayout) getActivity().findViewById(R.id.fragContainer);
		viewPager = (ViewPager) getActivity().findViewById(R.id.pager);

		view = inflater.inflate(R.layout.frag_one, container, false);
		btn1 = (Button) view.findViewById(R.id.menu_button1);
		btn2 = (Button) view.findViewById(R.id.menu_button2);

		//menu item 1 click
		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {				

				Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right_to_middle);
				animation.setAnimationListener(new AnimationListener() {                  
					@Override
					public void onAnimationStart(Animation animation) {
						//btn1.setEnabled(false);
						//btn2.setEnabled(false);
						//viewPager.setVisibility(View.GONE);

						fragContainer.setVisibility(View.VISIBLE);
						Frag3 frag3= new Frag3();
						FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
						fragTransaction.replace(R.id.fragContainer, frag3);
						fragTransaction.addToBackStack(null);
						fragTransaction.commit();	
					}

					@Override
					public void onAnimationRepeat(Animation animation) {

					}

					@Override
					public void onAnimationEnd(Animation animation) {
						viewPager.setVisibility(View.GONE);
					}
				});
				fragContainer.clearAnimation();
				fragContainer.startAnimation(animation);





				/*AlphaAnimation fadeOutAnimation = new AlphaAnimation(0, 1); // start alpha, end alpha
				fadeOutAnimation.setDuration(500); // time for animation in milliseconds
				fadeOutAnimation.setFillAfter(true); // make the transformation persist
				fadeOutAnimation.setAnimationListener(new AnimationListener() {         
					@Override
					public void onAnimationEnd(Animation animation) {				    	
					}

					@Override
					public void onAnimationRepeat(Animation animation) { }

					@Override
					public void onAnimationStart(Animation animation) {				    	
						fragContainer.setVisibility(View.VISIBLE);

						Frag3 frag3= new Frag3();
						FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
						fragTransaction.replace(R.id.fragContainer, frag3);
						fragTransaction.addToBackStack(null);
						fragTransaction.commit();				    	
					}
				});
				fragContainer.setAnimation(fadeOutAnimation);*/





				/*				
				fragContainer.setVisibility(View.VISIBLE);

				Frag3 frag3= new Frag3();
				FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();

				//fragTransaction.setCustomAnimations(R.anim.slide_middle_to_left, android.R.anim.fade_out);

				fragTransaction.replace(R.id.fragContainer, frag3);
				fragTransaction.addToBackStack(null);
				fragTransaction.commit();		*/						
			}
		});

		btn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {				

				AlphaAnimation fadeOutAnimation = new AlphaAnimation(0, 1); // start alpha, end alpha
				fadeOutAnimation.setDuration(500); 		// time for animation in milliseconds
				fadeOutAnimation.setFillAfter(true); 	// make the transformation persist
				fadeOutAnimation.setAnimationListener(new AnimationListener() {         
					@Override
					public void onAnimationEnd(Animation animation) {				    	
					}

					@Override
					public void onAnimationRepeat(Animation animation) { }

					@Override
					public void onAnimationStart(Animation animation) {				    	
						fragContainer.setVisibility(View.VISIBLE);

						Frag4 frag4= new Frag4();
						FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
						fragTransaction.replace(R.id.fragContainer, frag4);
						fragTransaction.addToBackStack(null);
						fragTransaction.commit();				    	
					}
				});
				fragContainer.setAnimation(fadeOutAnimation);

			}
		});


		return view;
	}

	public static void updateFromActivity(){
		
		btn1.setText("fffffyeah!");
		
	}
	
}
